using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    [SerializeField]private Animator anim;
    [SerializeField]private Rigidbody2D rb;
    public Collider2D coll;
    public LayerMask ground;

    public AudioSource playJumpSource,hurtAudio,cherryAudio;
    public Text CherryNum;
    
    public int Cherry;

    public float speed = 10f;

    public float jumpforce = 400f;

    private bool isHurt = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
       
    }
    
    void FixedUpdate()
    {
        if(!isHurt){
            Movement();
        }
     
        SwitchAnim();
    }

    //移动函数
    void Movement(){
        //获取 -1 ~ 1之间的小数
        float horizontalMove = Input.GetAxis("Horizontal");
        //获取 -1 0 1 
        float facedircetion = Input.GetAxisRaw("Horizontal");
        //角色移动
        if(horizontalMove != 0){
            rb.velocity = new Vector2(horizontalMove*speed*Time.deltaTime,rb.velocity.y);
            anim.SetFloat("running",Mathf.Abs(horizontalMove));
        }
        //角色转向
        if(facedircetion != 0){
            transform.localScale = new Vector3(facedircetion,1,1);
        }
        //角色跳跃。
        if(Input.GetButtonDown("Jump")&&coll.IsTouchingLayers(ground)){
            playJumpSource.Play();  
            rb.velocity = new Vector2(rb.velocity.x,Time.deltaTime * jumpforce);
            anim.SetBool("jumping",true);
        }
        //角色趴下
        if((Input.GetKeyDown(KeyCode.S)&&coll.IsTouchingLayers(ground))){
            anim.SetBool("crouching",true);
            Debug.Log(anim.GetBool("crouching"));
        }
        //角色趴下
        if((Input.GetKeyUp(KeyCode.S)&&anim.GetBool("crouching"))){
            anim.SetBool("crouching",false);
        }
    }

    //动画变换函数
    void SwitchAnim(){
        anim.SetBool("idle",false);

        if(rb.velocity.y < 0.1f && ! coll.IsTouchingLayers(ground)){
            anim.SetBool("falling",true);
        }

        if(anim.GetBool("jumping")){
            if(rb.velocity.y < 0 ){
                anim.SetBool("jumping",false);
                anim.SetBool("falling",true);
            }
        }
        else if(isHurt){
             anim.SetBool("hurting",true);
            if(Mathf.Abs(rb.velocity.x)<0.1f){
                isHurt = false;
                anim.SetBool("hurting",false);
                anim.SetFloat("running",0);
                anim.SetBool("idle",true);
            }
        }
        else if(coll.IsTouchingLayers(ground)){
            anim.SetBool("falling",false);
            anim.SetBool("idle",true);
        }
        
    }

    //收集物品
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Collection"){
            Cherry ++;
            cherryAudio.Play();
            CherryNum.text = Cherry.ToString();
            Destroy(other.gameObject);
        }
    }

    //消灭敌人
    private void OnCollisionEnter2D(Collision2D other) {
         if(other.gameObject.tag == "Enemies"){
             if(anim.GetBool("falling")){
                EnemiesController EnemiesControllerClass = other.gameObject.GetComponent<EnemiesController>();
                if(EnemiesControllerClass){
                    EnemiesControllerClass.JumpOn();
                }
               
                rb.velocity = new Vector2(rb.velocity.x,Time.deltaTime * jumpforce);
                anim.SetBool("jumping",true);
             }else if(transform.position.x < other.gameObject.transform.position.x){
                isHurt = true;
                hurtAudio.Play();
                rb.velocity = new Vector2(-2,2);
             }else if(transform.position.x > other.gameObject.transform.position.x){
                isHurt = true;
                hurtAudio.Play();
                rb.velocity = new Vector2(2,2);
             }
             
        }
    }
}
